<script setup lang="ts">

import {onMounted} from "vue";
import {getInterviewMessages, getInterviewOld} from "@/http/m_api/interview";
import {storeToRefs} from "pinia";
import appStore from "@/store";
import {Action, ElMessage, ElMessageBox} from "element-plus";
import {closeGlobalLoading, HideNavbar, openGlobalLoading, showNavbar} from "@/utils/common";


onMounted(() => {
  document
      .querySelector('body')
      ?.setAttribute('style', 'background-color:var(--background-color);')
  if (sessionStorage.getItem('isReloading') === 'true') {
    sessionStorage.removeItem('isReloading'); // 清除标志
    router.push('/interview'); // 使用 Vue Router 进行重定向
  }

  window.addEventListener('beforeunload', () => {
    sessionStorage.setItem('isReloading', 'true');
  });


})

const starting = ref(false)

const {interviewNewStore} = storeToRefs(appStore.useInterviewJsonStore)
const m_data = ref()

const start = async () => {
  const token = localStorage.getItem('token');
  const userInfo = localStorage.getItem('userInfo');
  if(!token || !userInfo){
    ElMessage.error("请先登录！")
    return;
  }
  const data = await getInterviewOld();
  if(data.data && data.data.length !== 0){
    // 读取面试记录
    m_data.value = data.data[0];
    ElMessageBox.confirm(
        '检测到未完成的面试，是否继续？',
        '提示',
        {
          type: "info",
          confirmButtonText: '确认继续',
          cancelButtonText: '新建面试',
          distinguishCancelAndClose: true
        }
    ).then(async ()=>{
      openGlobalLoading()
      interviewNewStore.value.id = m_data.value.id
      interviewNewStore.value.status = '进行中'
      interviewNewStore.value.startTime = new Date(m_data.value.startTime)
      interviewNewStore.value.Settings = m_data.value.settings
      const data = await getInterviewMessages(interviewNewStore.value.id)
      interviewNewStore.value.Messages = data.data.map((item: any) => {
            if (item.audioUrl) {
              // 将 audioUrl 重命名为 audio_url
              item.audio_url = item.audioUrl;
              delete item.audioUrl; // 删除旧的 audioUrl 属性
            }
            return item;
          }
      )
      interviewNewStore.value.Messages.shift();
      starting.value = true;
      HideNavbar()
      router.push('/interview/main')
      closeGlobalLoading()

    }).catch((action: Action)=>{
      if(action === 'cancel'){
        starting.value = true;
        HideNavbar()
        router.push('/interview/reqChoose')
      }
    })
  }else{
    starting.value = true;
    HideNavbar()
    router.push('/interview/reqChoose')
  }
}
const router = useRouter()
const route = useRoute()
const cancel = () => {
  router.push('/interview')
  starting.value = false;
  showNavbar()
}








</script>

<template>
  <el-button v-if="starting && route.path.split('/').filter(Boolean).pop()!='main'"
             class="cancel" circle type="danger" plain
             @click="cancel">返回
  </el-button>
  <router-view @start="start"> </router-view>
</template>

<style scoped lang="css">
.cancel {
  width: 50px;
  height: 50px;
  position: absolute;
  left: 50px;
  top: 30px;
  z-index: 1000;
}



</style>